// pages/add-phone/add-phone.js
Page({
	data: {
		phoneNumber: '',
		verificationCode: '',
		sendingCode: false,
		codeSent: false,
		countdown: 0 // 新增倒计时状态
	},

	onLoad() {
		// 页面加载时执行
	},

	goBack() {
		wx.navigateBack()
	},

	getCode() {
		if (!this.data.phoneNumber) {
			wx.showToast({
				title: '请输入手机号码',
				icon: 'none'
			})
			return
		}
		
		// 验证手机号码格式
		const phoneRegex = /^1[3-9]\d{9}$/
		if (!phoneRegex.test(this.data.phoneNumber)) {
			wx.showToast({
				title: '请输入正确的手机号码',
				icon: 'none'
			})
			return
		}
		
		// 显示发送中状态
		this.setData({
			sendingCode: true
		})
		
		// 调用后端接口发送验证码
		wx.request({
			url: 'http://localhost:8002/user/send-verification-code',
			method: 'POST',
			data: {
				phoneNumber: this.data.phoneNumber
			},
			success: (res) => {
				console.log('发送验证码响应:', res.data);
				
				if (res.data.code === 200) {
					// 发送成功
					this.setData({
						sendingCode: false,
						codeSent: true
					});
					
					wx.showToast({
						title: '验证码已发送',
						icon: 'success'
					});
					
					// 开始倒计时
					this.startCountdown();
				} else {
					// 发送失败
					this.setData({
						sendingCode: false
					});
					
					wx.showToast({
						title: res.data.msg || '发送失败，请重试',
						icon: 'none'
					});
				}
			},
			fail: (err) => {
				console.error('发送验证码失败:', err);
				
				this.setData({
					sendingCode: false
				});
				
				wx.showToast({
					title: '网络错误，请重试',
					icon: 'none'
				});
			}
		});
	},
	
	// 开始倒计时
	startCountdown() {
		let countdown = 60;
		this.setData({
			countdown: countdown
		});
		
		const timer = setInterval(() => {
			countdown--;
			this.setData({
				countdown: countdown
			});
			
			if (countdown <= 0) {
				clearInterval(timer);
				this.setData({
					countdown: 0,
					codeSent: false
				});
			}
		}, 1000);
	},

	complete() {
		const { phoneNumber, verificationCode } = this.data;
		
		if (!phoneNumber || !verificationCode) {
			wx.showToast({
				title: '请填写完整信息',
				icon: 'none'
			});
			return;
		}
		
		// 验证验证码格式
		if (verificationCode.length !== 6) {
			wx.showToast({
				title: '验证码格式不正确',
				icon: 'none'
			});
			return;
		}
		
		// 调用后端验证接口
		wx.request({
			url: 'http://localhost:8002/user/verify-code',
			method: 'POST',
			data: {
				phoneNumber: phoneNumber,
				verificationCode: verificationCode,
				openid: wx.getStorageSync('userOpenid') // 从本地存储获取openid
			},
			success: (res) => {
				if (res.data.code === 200) {
					wx.showToast({
						title: '验证成功',
						icon: 'success'
					});
					
					// 延迟跳转，让用户看到成功提示
					setTimeout(() => {
						// 返回登录页面
						wx.navigateBack({
							success: () => {
								// 通知登录页面刷新数据
								const pages = getCurrentPages();
								const loginPage = pages.find(page => page.route === 'pages/login/login');
								if (loginPage) {
									loginPage.refreshUserData();
								}
							}
						});
					}, 1500);
				} else {
					wx.showToast({
						title: res.data.msg || '验证失败',
						icon: 'none'
					});
				}
			},
			fail: (err) => {
				console.error('验证验证码失败:', err);
				wx.showToast({
					title: '网络错误，请重试',
					icon: 'none'
				});
			}
		});
	}
}) 